<script setup lang="ts">
import type { FormInstance } from '@arco-design/web-vue'
import { Message } from '@arco-design/web-vue'
import { ScheduleJobApi } from '@/apis'
import { useForm } from '@/hooks'

const emit = defineEmits<{
  (e: 'change'): void
}>()

const visible = ref(false)
const loading = ref(false)
const readonly = ref(true)

const dataId = ref('')
const dataDetail = ref<ScheduleJobApi.ScheduleJobLogDetailResp>()

const isCreateMode = computed(() => dataId.value === '')
const title = computed(() => (isCreateMode.value ? '创建定时任务日志' : '修改定时任务日志'))

const formRef = ref<FormInstance>()

const { form, resetForm } = useForm({
  jobName: undefined,
  cron: undefined,
  paramJson: undefined,
  startTime: undefined,
  endTime: undefined,
  elapsedSeconds: undefined,
  runStatus: undefined,
  dataCount: undefined,
  resultMessage: undefined,
})

const rules: FormInstance['rules'] = {
}

// 查询详情
const getDataDetail = async () => {
  dataDetail.value = await ScheduleJobApi.getLogDetail(dataId.value)
  for (const key in form) {
    form[key] = dataDetail.value?.[key]
  }
  loading.value = false
}

// 打开详情
const open = async (id: string) => {
  dataId.value = id
  loading.value = true
  visible.value = true
  readonly.value = true
  getDataDetail()
}

// 打开编辑
const edit = async (id: string) => {
  dataId.value = id
  loading.value = true
  visible.value = true
  readonly.value = false
  getDataDetail()
}

// 打开新增
const create = async () => {
  dataId.value = ''
  loading.value = false
  visible.value = true
  readonly.value = false
}

// 保存
const onSave = async () => {
  const isInvalid = await formRef.value?.validate()
  if (isInvalid) {
    return false
  }
  if (isCreateMode.value) {
    await ScheduleJobApi.create(form)
    Message.success('添加成功')
  } else {
    await ScheduleJobApi.update(form, dataId.value)
    Message.success('修改成功')
  }
  emit('change')
  visible.value = false
  return true
}

// 取消
const onCancel = () => {
  readonly.value = true
  for (const key in form) {
    form[key] = dataDetail.value?.[key]
  }
  formRef.value?.clearValidate()
}

// 编辑
const onEdit = () => {
  readonly.value = false
}

// 重置
const reset = () => {
  formRef.value?.resetFields()
  resetForm()
}

defineExpose({ open, edit, create })
</script>

<template>
  <ProFormPanel
    v-model:visible="visible"
    mode="drawer"
    :title="title"
    :loading="loading"
    :footer="isCreateMode"
    @confirm="onSave"
    @close="reset"
  >
    <a-form ref="formRef" layout="vertical" :model="form" :rules :readonly>
      <a-card title="基础信息" class="section">
        <template #extra>
          <ReadonlyEditButton
            v-if="false"
            :readonly="readonly"
            :deleteable="false"
            @edit="onEdit"
            @cancel="onCancel"
            @save="onSave"
          />
        </template>
        <a-row :gutter="28">
          <readonly-form-item label="任务名称" field="jobName">
            <a-input v-model.trim="form.jobName" placeholder="请输入任务名称" />
          </readonly-form-item>
          <readonly-form-item label="定时表达式" field="cron">
            <a-input v-model.trim="form.cron" placeholder="请输入定时表达式" />
          </readonly-form-item>
          <readonly-form-item label="参数" field="paramJson" :span="24">
            <a-input v-model.trim="form.paramJson" placeholder="请输入参数" />
          </readonly-form-item>
        </a-row>
      </a-card>
      <a-card title="执行情况" class="section">
        <a-row :gutter="28">
          <readonly-form-item label="开始时间" field="startTime">
            <a-input v-model.trim="form.startTime" placeholder="请输入开始时间" />
          </readonly-form-item>
          <readonly-form-item label="结束时间" field="endTime">
            <a-input v-model.trim="form.endTime" placeholder="请输入结束时间" />
          </readonly-form-item>
          <readonly-form-item label="耗时(s)" field="elapsedSeconds">
            <a-input v-model="form.elapsedSeconds" placeholder="请输入耗时(s)" />
          </readonly-form-item>
          <readonly-form-item label="运行状态" field="runStatus">
            <a-input v-model.trim="form.runStatus" placeholder="请输入运行状态" />
            <template #readonly>
              <CellDict code="RunStatusEnum" :value="form.runStatus" />
            </template>
          </readonly-form-item>
          <readonly-form-item label="数据计数" field="dataCount">
            <a-input v-model.trim="form.dataCount" placeholder="请输入数据计数" />
          </readonly-form-item>
          <readonly-form-item label="执行结果信息" field="resultMessage" :span="24">
            <a-input v-model.trim="form.resultMessage" placeholder="请输入执行结果信息" />
          </readonly-form-item>
        </a-row>
      </a-card>
    </a-form>
  </ProFormPanel>
</template>

<style lang="less" scoped></style>
